home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / Gopher_1.12 / Utilities.c < prev    next >
Text File  |  1992-03-16  |  4KB  |  168 lines

  1. // Utilities.c
  2. //
  3. // All the code in this file is taken from the Unix client
  4. // written at the University of Minnesota.
  5.  
  6. #import "Utilities.h"
  7.  
  8. ///////////////////////////////////////////////////////////////
  9. // Creates a socket to host hostname at port port.
  10. // Returns the socket, or :
  11. // -1 : Unknown host
  12. // -2 : Socket call failed
  13. // -3 : Connect call failed
  14. // It is the responsibility of the caller to close the socket
  15. // when it is over with it.
  16.  
  17. int create_socket(char *hostname, int port)
  18. {
  19. int     sock;
  20. struct sockaddr_in server;
  21. struct hostent *hp;
  22.  
  23. if ((server.sin_addr.s_addr = inet_addr(hostname)) == -1)
  24.     {
  25.     if (hp = gethostbyname(hostname))
  26.         {
  27.         bzero((char *) &server, sizeof(server));
  28.         bcopy(hp->h_addr, (char *) &server.sin_addr, hp->h_length);
  29.         server.sin_family = hp->h_addrtype;
  30.           }
  31.     else
  32.         return(-1);
  33.     }
  34. else
  35.     server.sin_family = AF_INET;
  36.  
  37. server.sin_port = (unsigned short) htons(port);
  38.  
  39. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  40.     return(-2);
  41.  
  42. setsockopt(sock, SOL_SOCKET, ~SO_LINGER, 0, 0); 
  43. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 0, 0);
  44. setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 0, 0);
  45. if (connect(sock, &server, sizeof(server)) < 0)
  46.     {
  47.     close(sock);
  48.     return(-3);
  49.     }
  50.  
  51. return(sock);
  52. }
  53.  
  54. ////////////////////////////////////////////////////
  55. // Write "n" bytes to a descriptor.
  56. // Use in place of write() when fd is a stream socket
  57. //
  58. // We return the number of bytes written
  59.  
  60. int writen(int fd, char *ptr, int nbytes)
  61. {
  62. int nleft, nwritten;
  63.  
  64. nleft = nbytes;
  65. while(nleft > 0)
  66.     {
  67.     nwritten = write(fd, ptr, nleft);
  68.     if (nwritten <= 0)
  69.         return(nwritten);    /* error */
  70.  
  71.     nleft    -= nwritten;
  72.     ptr    += nwritten;
  73.     }
  74. return(nbytes - nleft);
  75. }
  76.  
  77. ////////////////////////////////////////////////////
  78. // Writestring uses the writen and strlen calls to write a
  79. // string to the file descriptor fd.  If the write fails
  80. // a -1 is returned. Otherwise zero is returned.
  81.  
  82. int writestring(int fd, char *stringptr)
  83. {
  84. int length;
  85.  
  86. length = strlen(stringptr);
  87. if (writen(fd, stringptr, length) != length)
  88.     return(-1);
  89. else
  90.     return(0);
  91. }
  92.  
  93. ////////////////////////////////////////////////////
  94. // Read a line from a descriptor.  Read the line one byte at a time,
  95. // looking for the newline.  We store the newline in the buffer,
  96. // then follow it with a null (the same as fgets(3)).
  97. // We return the number of characters up to, but not including,
  98. // the null (the same as strlen(3))
  99.  
  100. int readline(int fd, char *ptr, int maxlen)
  101. {
  102. int n;
  103. int rc;
  104. char c;
  105.  
  106. for (n=1; n < maxlen; n++)
  107.     {
  108.     if ( (rc = read(fd, &c, 1)) == 1)
  109.         {
  110.         *ptr++ = c;
  111.         if (c == '\n')
  112.             break;
  113.         }
  114.     else if (rc == 0)
  115.         {
  116.         if (n == 1)
  117.             return(0);    /* EOF, no data read */
  118.         else
  119.             break;        /* EOF, some data was read */
  120.         }
  121.     else 
  122.         return(-1);        /* error */
  123.     }
  124.  
  125. *ptr = 0;                 /* Tack a NULL on the end */
  126. return(n);
  127. }
  128.  
  129. ////////////////////////////////////////////////////
  130. // This strips off  CR's and LF's leaving us with a nice pure string.
  131.  
  132. void ZapCRLF(char *inputline)
  133. {
  134. char *cp;
  135.  
  136. cp = index(inputline, '\r');    /* Zap CR-LF */
  137. if (cp != NULL)
  138.     *cp = '\0';
  139. else
  140.     {
  141.     cp = index(inputline, '\n');
  142.     if (cp != NULL)
  143.         *cp = '\0';
  144.     }
  145. }
  146.  
  147.  
  148. ////////////////////////////////////////////////////////////////////////////////
  149. // This does a case independent str search
  150. // No it's not really efficent.
  151. //
  152.  
  153. char *strcasestr(char *inputline, char *match)
  154. {
  155.      int matchlen=0;
  156.      int i, inlen;
  157.  
  158.      matchlen = strlen(match);
  159.      inlen = strlen(inputline);
  160.  
  161.      for(i=0; i<inlen; i++) {
  162.           if (strncasecmp(inputline+i, match, matchlen)==0)
  163.                return(inputline+i);
  164.      }
  165.  
  166.      return(NULL);
  167. }
  168.